/**
 * @System abcs-aspchain
 * @Description: Copyright: Copyright (c) 2018
 * Company: Aspire Technologies
 * @author zhongzifeng
 * @date 2018/12/6 9:51
 * @version V1.0
 */
package com.asp.aspchain.transaction.config;

import com.asp.aspchain.core.dao.MgrAccountMapper;
import com.asp.aspchain.core.dao.MgrNodeMapper;
import com.asp.aspchain.core.model.MgrAccountModel;
import com.asp.aspchain.core.model.MgrNodeModel;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @ClassName: MinerListConfig
 * @Description: 读取储存矿工配置信息
 * @author zhongzifeng
 * @date 2018/12/6 9:51
 *
 */
@Component
@ConfigurationProperties(prefix="ethereum")
public class MinerListConfig {

    @Value("${ethereum.nodeid}")
    private String nodeId;

    @Autowired
    private MgrAccountMapper mgrAccountMapper;

    @Autowired
    private MgrNodeMapper mgrNodeMapper;

    private List<String> miner = new ArrayList<String>();

    public void setMiner(List<String> miner) {
        this.miner = miner;
    }

    public List<String> getMiner() {
        return miner;
    }

    public Map<String, MgrAccountModel> getMinerMap(){
        int size = miner.size();
        Map<String,MgrAccountModel> result = new HashMap<String,MgrAccountModel>(size);
        Wrapper<MgrAccountModel> wrapper = new EntityWrapper<MgrAccountModel>();
        for(int i=0; i<miner.size();i++){
            String str = miner.get(i);
            wrapper.eq("accoutkey",str);
            if(i >= miner.size()-1){
                continue;
            }
            wrapper.or();
        }
        List<MgrAccountModel> mgrAccountList = mgrAccountMapper.selectList(wrapper);
        for(MgrAccountModel mgrAccount : mgrAccountList){
            result.put(mgrAccount.getAccountid(),mgrAccount);
        }
        return result;
    }

    public MgrNodeModel getMgrNode(){
        return mgrNodeMapper.selectById(nodeId);
    }


}
